package com.zhuangjie.shop.handler.activity.handler.impl;

import com.zhuangjie.shop.entity.ShopActivityInstance;
import com.zhuangjie.shop.enums.ActivityType;
import com.zhuangjie.shop.handler.activity.ActivityEnv;
import com.zhuangjie.shop.handler.activity.handler.ActivityHandler;

import java.math.BigDecimal;

/**
 * 新人活动处理程序
 *
 * @author zhuangjie
 * @date 2024/06/14
 */
public class NewCustomerActivityHandler extends ActivityHandler {
    @Override
    public ActivityType activityType() {
        return ActivityType.NEW_CUSTOMER_DISCOUNT;
    }

    @Override
    public void checkActivityValid(ShopActivityInstance shopActivityInstance) {
        String value = shopActivityInstance.getValue();
        try {
            double doubleValue = Double.parseDouble(value);
            if (doubleValue <= 0) {
                throw new RuntimeException(activityType().getName()+"优惠值必须大于0");
            }
        }catch (Exception e) {
            throw new RuntimeException(activityType().getName()+"优惠值格式错误！");
        }
    }

    @Override
    public BigDecimal calculateDiscountsHandler(ActivityEnv env, ShopActivityInstance shopActivityInstance) {
        if (env.getIsNewCustomer() && env.getDishListSumPrice().doubleValue() >= Double.parseDouble(shopActivityInstance.getValue())) {
            return new BigDecimal(shopActivityInstance.getValue());
        }
        return null;
    }
}
